package com.ma.cardgame

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class SecondActivity : AppCompatActivity() {
    private lateinit var imgAvatar: ImageView
    private lateinit var tvUsername: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvAccount: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvEmail: TextView
    private lateinit var tvHobby: TextView
    private lateinit var tvScore: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_second)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 绑定视图
        imgAvatar = findViewById(R.id.img_avatar)
        tvUsername = findViewById(R.id.tv_username)
        tvGender = findViewById(R.id.tv_gender)
        tvAccount = findViewById(R.id.tv_account)
        tvPhone = findViewById(R.id.tv_phone)
        tvEmail = findViewById(R.id.tv_email)
        tvHobby = findViewById(R.id.tv_hobby)
        tvScore = findViewById(R.id.tv_score)

        // 初次加载资料
        loadProfile()

        // 按钮跳转
        findViewById<Button>(R.id.btn_edit_profile).setOnClickListener {
            startActivity(Intent(this, ThirdActivity::class.java))
        }
        findViewById<Button>(R.id.btn_play_game).setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
        }
    }

    override fun onResume() {
        super.onResume()
        // 从第三页返回后刷新资料与头像
        loadProfile()
    }

    private fun loadProfile() {
        val sp = getSharedPreferences("profile", MODE_PRIVATE)
        tvUsername.text = sp.getString("username", getString(R.string.placeholder_username))
        tvGender.text = sp.getString("gender", getString(R.string.placeholder_gender))
        tvAccount.text = sp.getString("account", getString(R.string.placeholder_account))
        tvPhone.text = sp.getString("phone", getString(R.string.placeholder_phone))
        tvEmail.text = sp.getString("email", getString(R.string.placeholder_email))
        tvHobby.text = sp.getString("hobby", getString(R.string.placeholder_hobby))
        tvScore.text = sp.getInt("score", 0).toString()

        val avatarUriStr = sp.getString("avatar_uri", null)
        if (!avatarUriStr.isNullOrBlank()) {
            try {
                imgAvatar.setImageURI(Uri.parse(avatarUriStr))
            } catch (_: Exception) { /* ignore */ }
        }
    }
}